package initialize

import (
	"context"
	"fmt"
	"github.com/robfig/cron/v3"

	"gitee.com/baal1990/epidemic/global"
	"gitee.com/baal1990/epidemic/utils"
)

func Timer() {
	var option []cron.Option
	if global.GVA_CONFIG.Timer.WithSeconds {
		option = append(option, cron.WithSeconds())
	}
	_, err := global.GVA_Timer.AddTaskByFunc(global.CRONTAB_NOTIFY, global.GVA_CONFIG.Timer.Spec, func() {
		utils.NotifyRelatedStaffCheckin(global.GVA_CONFIG.Timer.TestMobile, global.GVA_CONFIG.Timer.Test)
	}, option...)
	if err != nil {
		fmt.Println("add timer error:", err)
		return
	}
	status := global.GVA_REDIS.Get(context.Background(), global.REDIS_KEY_AUTO_NOTIFY_STATUS)
	if status.Val() == global.AUTO_NOTIFY_START {
		global.GVA_Timer.StartTask(global.CRONTAB_NOTIFY)
	} else {
		global.GVA_Timer.StopTask(global.CRONTAB_NOTIFY)
	}
}
